<?php

class Fw_Main extends Zend_Controller_Action
{
	public $_model;
	public $_newModel;
	public $module;
	public $id;

	public function getProjectConfig()
	{
		$_model = $this->_getModel('FwConfig','fw-config');
		$fwConfig = $_model->getRow('1');
		// $cookie = new Zend_Http_Cookie($fwConfig['name'], 'WTF is Value?', 'localhost', '', '/skeleton/private/data/');
	}

	public function validateUser()
	{
		if(FW_MODE === 'admin') {
			Zend_Loader::loadClass('Fw_Controller_Validate_Session');
			Fw_Controller_Validate_Session::isClient();
		}
	}
	
	protected function _getModel($module, $moduleDir = null)
    {
		$modelClass = 'Model_'.$module;
		if($moduleDir == null) {
			$moduleDir = $this->_request->getModuleName();
		}
        if (null === $this->_model) {
			require_once APPLICATION_PATH . 'modules/'.$moduleDir.'/models/'.$module.'.php';
            $this->_model = new $modelClass();
        }
        return $this->_model;
    }
	
	protected function _getNewModel($module)
    {
		$modelClass = 'Model_'.$module;
		$moduleDir = $this->_request->getModuleName();
        if (null === $this->_newModel) {
			// $modelExplode = substr($module, 0, 2);
			// $modelName = str_replace($modelExplode, $modelExplode.'-', $module);
			require_once APPLICATION_PATH . 'modules/'.$moduleDir.'/models/'.$module.'.php';
            $this->_newModel = new $modelClass();
        }
        return $this->_newModel;
    }
	
	public function createDir($module)
	{
		$_model = $this->_getModel($module);
		$seq = $_model->getLast('id','1');
		foreach($seq as $key) {
			$id = $key['id'];
		}
		if($id == 0) {
			$id = 1;
		}
		$createDir = mkdir(PROJECT_PATH .'private/data/'.$module.'/'.$id);
		if(!$createDir) {
			throw new Exception('Data directory could not be created.');
		}
	}
	
	public function uploadFile($module)
	{
		$upload = new Zend_File_Transfer_Adapter_Http();
		if($upload->isUploaded()) {
			$_model = $this->_getModel($module);
			$seq = $_model->getLast('id','1');
			foreach($seq as $key) {
				$id = $key['id'];
			}
			if($id == 0) {
				$id = 1;
			}
			
			$mimetype = array('image/bmp', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
			
			$upload->addValidator('MimeType', true, $mimetype);
			$upload->addFilter('Rename', PROJECT_PATH.'private/data/'.$module.'/'.$id.'/1.jpg');
			if(!$upload->isValid()) {
				throw new Exception('File is not valid.');
			}
			if(!$upload->receive()) {
				throw new Exception('Could not upload file to the directory.');
			}
			// Zend_Debug::dump($upload->getFileInfo());
		}
	}

	public function removeDir($module, $id)
	{
		$dirPath = PROJECT_PATH .'/private/data/'.$module.'/'.$id;
		$iterator = new DirectoryIterator($dirPath);
		foreach ($iterator as $fileinfo) {
			if ($fileinfo->isDir()) {
				$removeDir = rmdir($dirPath);
			}
		}
	}
	
	public function createRss($entries, $module)
	{
		// Create the RSS array
		$rss = array(
					'title'   => 'Skeleton',
					'link'    => 'http://localhost/skeleton/',
					'charset' => 'UTF-8',
					'language' => 'pt-br',
					'entries' => $entries
					);
		
		// Import the array
		$feed = Zend_Feed::importArray($rss, 'rss');
		
		// Write the feed to a variable
		$rssFeed = $feed->saveXML();
		
		// Write the feed to a file residing in /public/rss
		$fh = fopen(PROJECT_PATH.'private/data/'.$module.'/feed.rss', "w");
		fwrite($fh, $rssFeed);
		fclose($fh);
	}
}